﻿using System;
using PropertyExpression.Common;

namespace PropertyExpression.ControllableQuery.Demo
{
    class SomeClass2
    {
        void Method1()
        {
            Filter? filter = Filter.Suggested;
            Option<string> search = "%test%";
            var paging = BatchQuery<Paging<ITagSynonym, int?>>.New(new {
                filter, userIsAnonymous = false, userIsModerator = false, userId = 1, tagScoreRequiredToVote = 10,
                search, tab = Tab.Newest, start = 10, finish = 20
            }, @"
@Paging.Get(mainQuery: @<text>
SELECT  TagSynonyms.Id, SourceTagName, TargetTagName, OwnerUserId, ApprovalDate, TagSynonyms.CreationDate,
        AutoRenameCount, Score, SourceTags.Id AS SourceTagId, TargetTags.Id AS TargetTagId, Users.DisplayName
FROM    TagSynonyms
        LEFT JOIN Tags SourceTags ON SourceTagName = SourceTags.Name
        LEFT JOIN Tags TargetTags ON TargetTagName = TargetTags.Name
        LEFT JOIN Users ON Users.Id = OwnerUserId
WHERE   1 = 1
@switch (filter) {
    case Filter.Active:
        @:AND ApprovalDate IS NOT NULL
        break;
    case Filter.Suggested:
        @:AND ApprovalDate IS NULL
        if (!userIsAnonymous && !userIsModerator) {
        @:AND TargetTagName IN (@TargetTagNames())
        }
        break;
    case Filter.Merge:
        @:AND ApprovalDate IS NOT NULL AND ISNULL(SourceTags.Count, 0) > 0
        break;
}
@if (search.HasValue) {
        @:AND (SourceTagName LIKE @search OR TargetTagName LIKE @search)
}</text>,
    orderBy: @<text>
@switch (tab) {
    case Tab.Newest:
        @:CreationDate DESC
        break;
    case Tab.Master:
        @:TargetTagName ASC, AutoRenameCount DESC
        break;
    case Tab.Synonym:
        @:SourceTagName ASC, AutoRenameCount DESC
        break;
    case Tab.Votes:
        @:Score DESC, TargetTagName ASC, AutoRenameCount DESC
        break;
    case Tab.Creator:
        @:DisplayName
        break;
    case Tab.Renames:
        @:AutoRenameCount DESC, TargetTagName
        break;
}</text>,
    start: @start, finish: @finish)
@helper TargetTagNames() {<text>
SELECT  Name
FROM    Tags
WHERE   Id IN (SELECT   Id
                FROM    UserTagTotals
                WHERE   UserId = @userId
                        AND TotalAnswerScort > @tagScoreRequiredToVote)
</text>}").Result();
            var rows = paging.Rows;
            var count = paging.Count;
        }
    }

    public interface ITagSynonym
    {
        int Id { get; }
        string SourceTagName { get; }
        string TargetTagName { get; }
        int? OwnerUserId { get; }
        DateTime? ApprovalDate { get; }
        DateTime? CreationDate { get; }
        int? AutoRenameCount { get; }
        int? Score { get; }
        int? SourceTagId { get; }
        int? TargetTagId { get; }
        string DisplayName { get; }
        long? RowNumber { get; }
    }

    public enum Tab
    {
        Newest,
        Master,
        Synonym,
        Votes,
        Creator,
        Renames
    }

    public enum Filter
    {
        Active,
        Suggested,
        Merge
    }
}